package workflow.dao;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import org.springside.core.dao.HibernateEntityDao;
import org.hibernate.Query;

import workflow.model.ProcessVariable;

import common.PageInfo;

public class ProcessVariableDao extends HibernateEntityDao<ProcessVariable>{
	public List<ProcessVariable> findList(HashMap<String, String> hm, PageInfo pageInfo){
		try{
		
		String listHql = "FROM ProcessVariable po WHERE (1=1) ";
		String countHql = null;
		/*------------查询条件-------------*/
		//注意以设值方式组装查询条�?,参�?�PageItemManager 方法:findList
		
		String id = (String)hm.get("id");
		String proInsId = (String)hm.get("proInsId");
		String taskId = (String)hm.get("taskId");
		String varName = (String)hm.get("varName");
		String varValue = (String)hm.get("varValue");


		int count[] = {0, 0, 0, 0, 0};

		if(id != null && !"".equals(id.trim())){
			listHql += " AND id =:id";
			count[0] = 1;
		}

		if(proInsId != null && !"".equals(proInsId.trim())){
			listHql += " AND proInsId =:proInsId";
			count[1] = 1;
		}

		if(taskId != null && !"".equals(taskId.trim())){
			listHql += " AND taskId =:taskId";
			count[2] = 1;
		}

		if(varName != null && !"".equals(varName.trim())){
			listHql += " AND varName LIKE:varName";
			count[3] = 1;
		}

		if(varValue != null && !"".equals(varValue.trim())){
			listHql += " AND varValue LIKE:varValue";
			count[4] = 1;
		}

		listHql += " ORDER BY po.id DESC";
		countHql = listHql;
		countHql = countHql.replace("FROM ProcessVariable", "SELECT COUNT(*) FROM ProcessVariable");
		Query queryCount = this.createQuery(countHql);
		Query queryList = this.createQuery(listHql);

		if(count[0] == 1){
			queryList.setLong("id", Long.valueOf(id));
			queryCount.setLong("id", Long.valueOf(id));
		}

		if(count[1] == 1){
			queryList.setLong("proInsId", Long.valueOf(proInsId));
			queryCount.setLong("proInsId", Long.valueOf(proInsId));
		}

		if(count[2] == 1){
			queryList.setLong("taskId", Long.valueOf(taskId));
			queryCount.setLong("taskId", Long.valueOf(taskId));
		}

		if(count[3] == 1){
			queryList.setString("varName", "%" + varName + "%");
			queryCount.setString("varName", "%" + varName + "%");
		}

		if(count[4] == 1){
			queryList.setString("varValue", "%" + varValue + "%");
			queryCount.setString("varValue", "%" + varValue + "%");
		}



		//分页查询-START
		List countList = queryCount.list();
		int totalSize = 0;//总数
		if(countList.size() > 0){
			totalSize = Integer.valueOf(countList.get(0).toString());
			pageInfo.setTotalSize(totalSize);
		} else {
			pageInfo.setTotalSize(0);
		}
		
		queryList.setMaxResults(pageInfo.getPageSize());//设置查询数量
		queryList.setFirstResult(pageInfo.getPageNO() * pageInfo.getPageSize());//设置查询分段
		//分页查询-END
		
		return queryList.list();
		
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<ProcessVariable>();
		}
	}
	
	public boolean deleteByIds(String ids){
		boolean isSuccessful = false;
		try{			
			String hql = "DELETE FROM ProcessVariable po WHERE po.id IN (" + ids + ")";
			this.getHibernateTemplate().bulkUpdate(hql);
			isSuccessful = true;
		} catch (Exception e){
			e.printStackTrace();
		}
		
		return isSuccessful;
	}
}
